<template>
  <div>
    <q-form class="dialog_card column" ref="editRef">
      <div class="row q-col-gutter-x-md dialog_form q-pa-md">
        <div class="col-12">
          <h5>
            <q-icon color="red" name="star" />
            类型名称：
          </h5>
          <q-input
            :rules="[(val) => (val && val.length > 0) || '请输入类型名称']"
            clearable
            dense
            lazy-rules
            outlined
            type="text"
            v-model="form.tag_name"
          />
        </div>

        <div class="col-12">
          <h5>
            <q-icon color="red" name="star" />
            排序：
          </h5>
          <q-input
            :rules="[(val) => (val && val > 0) || '请输入大于0的排序']"
            clearable
            dense
            lazy-rules
            outlined
            type="number"
            v-model.number="form.tag_sort"
          />
        </div>

        <div class="col-12">
          <h5>是否启用：</h5>
          <q-toggle
            :false-value="0"
            :true-value="1"
            color="bule"
            v-model="form.tag_state"
          />
        </div>
      </div>
    </q-form>
  </div>
</template>

<script lang="ts" setup>
import emitter from '@/components/eventbus';
import { goodsTagAdd, goodsTagEdit } from '@/api/goods';
import { useQuasar } from 'quasar';

interface Props {
  form: {
    id?: number;
    tag_name?: string;
    tag_state?: number;
    tag_sort?: number;
    created_at?: string;
  };
}

const props = withDefaults(defineProps<Props>(), {
  form: () => ({}),
});
const editRef = ref();
const $q = useQuasar();

//接收兄弟组件传递事件
emitter.on('saveData', async () => {
  IsValidate();
});
//表单验证
const IsValidate = () => {
  if (editRef.value) {
    editRef.value.validate().then(async function (validate: any) {
      if (validate) {
        onSubmit();
      } else {
      }
    });
  }
};
//提交数据
const onSubmit = async () => {
  if (form.value.id) {
    const res: any = await goodsTagEdit(form.value);
    info(res);
  } else {
    const res: any = await goodsTagAdd(form.value);
    info(res);
  }
};
//信息返回
const info = (res: any) => {
  if (res.code == 200) {
    $q.notify({
      color: 'white',
      textColor: 'positive',
      icon: 'check_circle',
      position: 'top',
      message: res.msg,
    });
    emit('backList');
  }
};

const emit = defineEmits(['update:form', 'backList']);

const form: any = computed({
  set(value: any) {
    // emit('update:form', value);
  },
  get() {
    return props.form;
  },
});
</script>
<style lang="scss" scoped></style>
